package com.peas.platform.common.core.configuration;

import cn.hutool.core.util.ClassUtil;
import com.peas.platform.common.core.enums.BaseEnum;
import org.springframework.format.Formatter;
import org.springframework.lang.NonNull;

import java.util.Locale;
import java.util.Objects;

public class FormatterBaseEnum<E extends BaseEnum> implements Formatter<E> {

	@SuppressWarnings("unchecked")
	@NonNull
	@Override
	public E parse(@NonNull String text, @NonNull Locale locale) {
		Class<E> clazz = (Class<E>) ClassUtil.getTypeArgument(getClass(), 1);
		return Objects.requireNonNull(BaseEnum.key2Enum(clazz, text));
	}

	@NonNull
	@Override
	public String print(@NonNull E object, @NonNull Locale locale) {
		return object.getKey();
	}

}
